"use strict";
/* Controllers */
app.controller("olapConfigEditController", ['$scope', '$location', '$rootScope', '$q','$routeParams', 'olapConfigEditService', function ($scope, $location, $rootScope, $q, $routeParams, olapConfigEditService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.id = $routeParams.id; // 如果id!=0为更新

    $scope.getOlapConfig = function () {
        var vm = $scope.vm;
        var params = {
            id: vm.p.id
        };
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.p = res.data[0];
                vm.p.timeInterval=vm.p.timeInterval*1
                vm.p.timeGran=vm.p.timeGran*1
                $('#dimensions').tagsinput('add', vm.p.dimensions);
                $('#indicators').tagsinput('add', vm.p.indicators);
            } else {
                alert(res.msg);
            }
        };
        return olapConfigEditService.getOlapConfig(params, procFn, errorFn);
    };

    $('#dimensions').on('itemAdded', function(event) {
        if(typeof(event.options)!='undefined'&&event.options.preventPost){
            return;
        }
        var arr=$("#dimensions").tagsinput('items')
        var resArr = getNewObjectOf(arr.sort());
        $('#dimensions').tagsinput('removeAll');
        for(var i in resArr){
          $('#dimensions').tagsinput('add', resArr[i],{preventPost: true});
        }
    });


    $('#indicators').on('itemAdded', function(event) {
        var tag = event.item;
        var  objRegExp= /^\w+_\w+$/;
        if (!event.options || !event.options.preventPost) {
            var res=objRegExp.test(tag);
            if(!res){
                $('#indicators').tagsinput('remove', tag, {preventPost: true});
                alert("输入的内容不符合wtable的特征规范，请重新填写")
            }
        }

    });

    if(vm.p.id!=0) {
        $scope.getOlapConfig()
    }

    function getNewObjectOf(src) {
      return Object.assign({}, src);
    }

    //e_time\s*[\)]?\s*[>|>=|<|<=|=]{1,2}\s*['|"]{1}%s['|"]{1}
    var errorFn = function (result) {
        alert(result);
    };

    $scope.olapConfigAdd = function () {
        var vm = $scope.vm;
        var params = vm.p;
        params.sql=params.sql.replaceAll("'","\\'")
        var procFn = function (response) {
            params.sql=params.sql.replaceAll("\\'","'")
            if (response.success && response.data) {
                alert("更新成功！")
            } else {
                alert(response.msg);
            }
        };
        olapConfigEditService.olapConfigUpdate(params, procFn, errorFn);
    };


}]);


/* Services */
app.service('olapConfigEditService', ['ajaxService', function (ajaxService) {

    this.olapConfigUpdate = function (params, successFunction, errorFunction) {
        console.log(params)
        ajaxService.AjaxPost(params, "/star/api/rt-olap-service/api/alarm/olapSqlConf/update", successFunction, errorFunction);
    };

    this.getOlapConfig = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/rt-olap-service/api/alarm/olapSqlConf/get/"+params.id, successFunction, errorFunction);
    };

}]);

